<?php
/**
 * 用户管理
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 *
 * @author      cmpan
 * @copyright   Copyright (c) 2009-2010 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
class AdminController extends UE_Controller_Admin {
	/**
	 * 角色
	 *
	 * @var RoleModel
	 */
	private $_role;
	
	/**
	 * 角色
	 *
	 * @var UserModel
	 */
	private $_user;
	
	public function init() {		
		UE_Loader::loadModel('RoleModel');
		UE_Loader::loadModel('UserModel');
	    $this->_role = new RoleModel();
	    $this->_user = new UserModel();
	}
	
	public function listAction() {
		
		if (IS_POST) {
			if (!empty($_POST['itemcheck']) && !empty($_POST['optype']) && in_array($_POST['optype'], array('disable', 'enable', 'del'))) {
				$superId = UE_Config::get('super_admin_id');
				if (in_array($superId, $_POST['itemcheck'])) {
					$this->setOK('超级管理员没有被修改。');
				}
				switch ($_POST['optype']){
					case 'disable':
						$ids = join(',', $_POST['itemcheck']);
						$sql = "UPDATE ue_user SET user_enabled=0 WHERE user_id IN($ids) AND user_id !='$superId'";
						UE_DB::factory()->query($sql);
						break;
					case 'enable':
						$ids = join(',', $_POST['itemcheck']);
						$sql = "UPDATE ue_user SET user_enabled=1 WHERE user_id IN($ids) AND user_id !='$superId'";
						UE_DB::factory()->query($sql);
						break;
					case 'del':
						$ids = join(',', $_POST['itemcheck']);
						$sql = "DELETE FROM ue_user WHERE user_id IN($ids) AND user_id !='$superId'";
						UE_DB::factory()->query($sql);
						break;
				}
				
				$this->setOK('操作成功。');				
			} else {
				$this->setWarn('没有进行任何处理。');
			}
		}
		
		$sql = "SELECT * FROM ue_user WHERE user_type='admin' OR user_type='super'";
		$admins = UE_DB::factory()->getAll($sql);
		//print_r($admins);
		
		include $this->tpl();
	}
	
	public function addAction() {
		$adminRoles = $this->_role->getRoles('admin', false);
		
		if (IS_POST) {
			if($this->_user->add($_POST)) {
				$this->setOK('成功添加管理员');
			} else {
				$this->setErr($this->_user->getErrs());
			}
		}
		
	    // 添加管理员
	    include $this->tpl();
	}
	
	public function editAction() {
		$this->id = (float)$this->id;
		if (empty($this->id)) {
			print 'Error Param!';
			return false;
		}
		$superId = UE_Config::get('super_admin_id');
		
		if ($this->id == $superId && $_SESSION['user_id'] != $this->id) {
			$this->setErr('只有超级管理员才能修改该账户。');			
		}
		
		$adminRoles = $this->_role->getRoles('admin', false);
		$this->_user->setId($this->id);
		
		if (IS_POST) {
			if($this->_user->update($_POST)) {
				$this->setOK('成功编辑管理员');
			} else {
				$this->setErr($this->_user->getErrs());
			}
		}
		
		$user = $this->_user->read();
		$user['user_role_array'] = $user['user_role'] ? explode(',', $user['user_role']) : array();
		
		include $this->tpl();
	}
	
	public function delAction() {
	    $this->_user->setId($this->id)->delete();
	    header('Location: '.UE::referer());
	}	
}